package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.Notice;
import com.project.entity.User;
import com.project.myBeans.ResponseBean;
import com.project.service.NoticeService;
import com.project.service.UserService;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * author: WheelChen
 * data: 2017/6/26
 * function: 通知操作
 */
@Controller
@RequestMapping("/notice")
public class NoticeController extends BaseService {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/findByCondition")
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/26
     * function: 分页条件查找
     *
     * @param notice
     * @return
     */
    public ResponseBean findByCondition(@RequestBody Notice notice) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (notice.getPageIndex() > 0)
                notice.setPageIndex(notice.getPageIndex() - 1);
            List<Notice> notices = noticeService.findByCondition(notice);
            responseBean.setData(notices);
            responseBean.setTotal(notice.getTotal());
            responseBean.setMessage("获取成功");
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
        }
        return responseBean;

    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据id查找通知信息
     *
     * @param id
     * @return
     */
    public ResponseBean findById(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        JSONObject jsonObject = new JSONObject();
        User user;
        try {
            Notice notice = noticeService.findById(id);
            jsonObject.put("notice", notice);
            if (notice != null) {
                user = userService.findOneByUserID(notice.getComposerid());
                if (user != null) {
                    jsonObject.put("composerName", user.getRealname());
                }
                responseBean.setData(jsonObject);
                responseBean.setMessage("查找成功");
                responseBean.setSuccess(true);
            }
            else {
                responseBean.setMessage("该通知不存在");
                responseBean.setSuccess(false);
            }
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/removeById/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 根据id删除通知信息
     *
     * @param id
     * @return
     */
    public ResponseBean removeById(@PathVariable("id") Integer id) {
        ResponseBean responseBean = new ResponseBean();
        try {
            processSuccessDeleteUpdate(noticeService.delete(id), responseBean);
            return responseBean;
        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/6/30
     * function: 添加通知
     *
     * @param notice
     * @return
     */
    public ResponseBean add(@RequestBody Notice notice) {
        ResponseBean responseBean = new ResponseBean();
        try {
            noticeService.add(notice);
            responseBean.setMessage("发送成功");
            return responseBean;

        } catch (Exception e) {
            responseBean.setSuccess(false);
            responseBean.setMessage("程序异常");
            return responseBean;
        }
    }
}
